<template>
	<view class="mainpadding2">
		<view class="mainpadding radius ffffff margin_top" v-for="(item,index) in list" :key="index">
			<!-- 大单位 -->
			<view class="titletext xiabiank">{{item.name}}</view>
			<template v-if="item.biggoodsunit.id">
				<view class="flexbuju margin_top" v-if="item.producedate_status==1" @click="selectTime(index,1)">
					<view class="huisetext">生产日期：</view>
					<input type="text" class="inputs xiabiank" style="text-align: center;" placeholder="选择生产日期"
						disabled="" v-model="item.biggoodsunit.produce_time">
					<view class="sanjiaox"></view>
				</view>
				<view class="flexbetween margin_top">
					<input type="number" class="inputsl xiabiank" placeholder="数量" v-model="item.biggoodsunit.num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.biggoodsunit.name}}</view>
					<input type="number" class="inputsl xiabiank" placeholder="单价" v-model="item.biggoodsunit.price" >
					<input type="number" class="inputsl xiabiank" placeholder="金额" style="margin-right: 10rpx;"
						:value="total_price(item.biggoodsunit.num,item.biggoodsunit.price)" disabled="" >
					<input type="text" class="inputsl xiabiank" placeholder="备注" v-model="item.biggoodsunit.notes">
				</view>
				<!-- 赠品信息，销售单可添加赠品（上方按钮如果为退货状态不可以添加赠品） -->
				<view class="flexleft margin_top">
					<input type="number" class="xiabiank textcontent" placeholder="赠品数"
						v-model="item.biggoodsunit.send_num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.biggoodsunit.name}}</view>
					<input type="text" class="xiabiank textcontent" placeholder="赠品备注"
						v-model="item.biggoodsunit.send_notes">
				</view>
			</template>
			<!-- 中单位 -->
			<template v-if="item.middlegoodsunit.id">
				<!-- 自选生产日期 -->
				<view class="flexbuju margin_top" v-if="item.producedate_status==1" @click="selectTime(index,2)">
					<view class="huisetext">生产日期：</view>
					<input type="text" class="inputs xiabiank" style="text-align: center;" placeholder="选择生产日期"
						disabled="" v-model="item.middlegoodsunit.produce_time">
					<view class="sanjiaox"></view>
				</view>
				<view class="flexbetween margin_top">
					<input type="number" class="inputsl xiabiank" placeholder="数量" v-model="item.middlegoodsunit.num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.middlegoodsunit.name}}
					</view>
					<input type="number" class="inputsl xiabiank" placeholder="单价" v-model="item.middlegoodsunit.price" >
		
					<input type="number" class="inputsl xiabiank" placeholder="金额" style="margin-right: 10rpx;"
						:value="total_price(item.middlegoodsunit.num,item.middlegoodsunit.price)" disabled="" >
					<input type="text" class="inputsl xiabiank" placeholder="备注" v-model="item.middlegoodsunit.notes">
				</view>
				<!-- 赠品信息，销售单可添加赠品（上方按钮如果为退货状态不可以添加赠品） -->
				<view class="flexleft margin_top">
					<input type="number" class="xiabiank textcontent" placeholder="赠品数"
						v-model="item.middlegoodsunit.send_num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.middlegoodsunit.name}}
					</view>
					<input type="text" class="xiabiank textcontent" placeholder="赠品备注"
						v-model="item.middlegoodsunit.send_notes">
				</view>
			</template>
			<!-- 小单位 -->
			<template v-if="item.smallgoodsunit.id">
				<!-- 自选生产日期 -->
				<view class="flexbuju margin_top" v-if="item.producedate_status==1" @click="selectTime(index,3)">
					<view class="huisetext">生产日期：</view>
					<input type="text" class="inputs xiabiank" style="text-align: center;" placeholder="选择生产日期"
						disabled="" v-model="item.smallgoodsunit.produce_time">
					<view class="sanjiaox"></view>
				</view>
				<view class="flexbetween margin_top">
					<input type="number" class="inputsl xiabiank" placeholder="数量" v-model="item.smallgoodsunit.num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.smallgoodsunit.name}}</view>
					<input type="number" class="inputsl xiabiank" placeholder="单价" v-model="item.smallgoodsunit.price" >
					<input type="number" class="inputsl xiabiank" placeholder="金额" style="margin-right: 10rpx;"
						:value="total_price(item.smallgoodsunit.num,item.smallgoodsunit.price)" disabled="" >
					<input type="text" class="inputsl xiabiank" placeholder="备注" v-model="item.smallgoodsunit.notes">
				</view>
				<!-- 赠品信息，销售单可添加赠品（销售单上方按钮如果为退货状态不可以添加赠品） -->
				<view class="flexleft margin_top" >
					<input type="number" class="xiabiank textcontent" placeholder="赠品数"
						v-model="item.smallgoodsunit.send_num">
					<view class="nostrongtext flexcenter" style="min-width: 100rpx;">{{item.smallgoodsunit.name}}</view>
					<input type="text" class="xiabiank textcontent" placeholder="赠品备注"
						v-model="item.smallgoodsunit.send_notes">
				</view>
			</template>
		</view>
		<view class="mainpadding_top posibottom">
			<view class="submitbtn" @click="submit">确定</view>
		</view>
		<u-datetime-picker :show="timeshow" mode="date" v-model="value1" title="选择生产日期" @confirm="timersure"
			@cancel="timeshow=false">
		</u-datetime-picker>
	</view>
</template>

<script>
	import httpRequest from '@/commonFun/httpRequest.js'
	export default {
		data() {
			return {
				getmsg:{
					ids:"",
					type:""
				},
				list: [],
				selectindex: "", //选择生产日期下标标识
				selectvalue: "", //选择生产日期大中小单位标识
				timeshow: false,
				value1: Number(new Date()),
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.getmsg));
			this.getmsg = data
			this.init()
		},
		methods: {
			submit() {
				let arr = []
				for (let i = 0; i < this.list.length; i++) {
					let item = this.list[i]
					console.log(item,"999999");
					// 向数组中添加大中小商品
					if (item.biggoodsunit.num != "") {
						let biggoodsunitData = {
							goods_id: item.id,
							price: item.biggoodsunit.price,
							num: item.biggoodsunit.num,
							produce_time: item.biggoodsunit.produce_time,
							goods_sku_id: item.biggoodsunit.goods_sku_info.id,
							goods_sku_name: item.biggoodsunit.name,
							notes: item.biggoodsunit.notes,
							goodsinfo: item,
							send_num: item.type == 1 ? item.biggoodsunit.send_num : 0, //判断当前是否是退货商品，如果为退货商品赠送数量为0
							send_notes: item.biggoodsunit.send_notes,
						}
						arr.push(biggoodsunitData)
					}
					if (item.middlegoodsunit.num != "") {
						let middlegoodsunitData = {
							goods_id: item.id,
							price: item.middlegoodsunit.price,
							num: item.middlegoodsunit.num,
							produce_time: item.middlegoodsunit.produce_time,
							goods_sku_id: item.middlegoodsunit.goods_sku_info.id,
							goods_sku_name: item.middlegoodsunit.name,
							notes: item.middlegoodsunit.notes,
							goodsinfo: item,
							send_num: item.type == 1 ? item.middlegoodsunit.send_num : 0,
							send_notes: item.middlegoodsunit.send_notes,
						}
						arr.push(middlegoodsunitData)
					}
					if (item.smallgoodsunit.num != "") {
						let smallgoodsunitData = {
							goods_id: item.id,
							price: item.smallgoodsunit.price,
							num: item.smallgoodsunit.num,
							produce_time: item.smallgoodsunit.produce_time,
							goods_sku_id: item.smallgoodsunit.goods_sku_info.id,
							goods_sku_name: item.smallgoodsunit.name,
							notes: item.smallgoodsunit.notes,
							goodsinfo: item,
							send_num: item.type == 1 ? item.smallgoodsunit.send_num : 0,
							send_notes: item.smallgoodsunit.send_notes,
						}
						arr.push(smallgoodsunitData)
					}
				}
				if (!arr.length) {
					httpRequest.toast("请输入数量！")
					return false
				}
				console.log(arr,"00000");
				let tuihuodanInfo = JSON.parse(uni.getStorageSync("tuihuodanInfo"))
				let concatarr = tuihuodanInfo.data.concat(arr)
				tuihuodanInfo.data = tuihuodanInfo.data.concat(arr)
				uni.setStorageSync('tuihuodanInfo', JSON.stringify(tuihuodanInfo))
				uni.navigateBack({
					delta: 2
				})
			},
			init() {
				let _this = this;
				let data = {
					ids: this.getmsg.ids,
					warehouse_id: this.getmsg.warehouse_id
				}
				httpRequest.request('/customapi/goods/chooseGoodsList', 'POST', data, false, false, true).then(res => {
					res.data.forEach(item => {
						if(item.biggoodsunit.goods_sku_info){
							// 大单位
							this.$set(item.biggoodsunit, "price", item.biggoodsunit.goods_sku_info
								.prev_xs_price ? item.biggoodsunit.goods_sku_info.prev_xs_price : item
								.biggoodsunit.goods_sku_info.price) //单价,判断是否存在上次购买价格，存在拿采购价格否则拿单价
							this.$set(item.biggoodsunit, "produce_time", "") // 生产日期
							this.$set(item.biggoodsunit, "notes", "") // 备注
							this.$set(item.biggoodsunit, "num", "") // 数量
							this.$set(item.biggoodsunit, "total_price", "") // 总价
						}
						if(item.middlegoodsunit.goods_sku_info){
							// 中单位
							this.$set(item.middlegoodsunit, "price", item.middlegoodsunit.goods_sku_info
								.prev_xs_price ? item.middlegoodsunit.goods_sku_info.prev_xs_price :
								item
								.middlegoodsunit.goods_sku_info.price
							) //单价,判断是否存在上次采购价格，存在拿采购价格否则拿进价
							this.$set(item.middlegoodsunit, "produce_time", "") // 生产日期
							this.$set(item.middlegoodsunit, "notes", "") // 备注
							this.$set(item.middlegoodsunit, "num", "") // 数量
							this.$set(item.middlegoodsunit, "total_price", "") // 总价
						}
						// 小单位
						this.$set(item.smallgoodsunit, "price", item.smallgoodsunit.goods_sku_info
							.prev_xs_price ? item.smallgoodsunit.goods_sku_info.prev_xs_price :
							item
							.smallgoodsunit.goods_sku_info.price
						) //单价,判断是否存在上次采购价格，存在拿采购价格否则拿进价
						this.$set(item.smallgoodsunit, "produce_time", "") // 生产日期
						this.$set(item.smallgoodsunit, "notes", "") // 备注
						this.$set(item.smallgoodsunit, "num", "") // 数量
						this.$set(item.smallgoodsunit, "total_price", "") // 总价
						// 销售单，需要展示，赠品
						if (this.getmsg.type == 3 || this.getmsg.type == 4) {
							this.$set(item, "type", 1) // 商品类型，1销售商品，2退货商品
							this.$set(item.biggoodsunit, "send_notes", "") // 赠送备注
							this.$set(item.biggoodsunit, "send_num", "") // 赠送数量
							this.$set(item.middlegoodsunit, "send_notes", "") // 赠送备注
							this.$set(item.middlegoodsunit, "send_num", "") // 赠送数量
							this.$set(item.smallgoodsunit, "send_notes", "") // 赠送备注
							this.$set(item.smallgoodsunit, "send_num", "") // 赠送数量
						}
					})
					this.list = res.data
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			timersure(e) {
				let obj = {
					"1": "biggoodsunit",
					"2": "middlegoodsunit",
					"3": "smallgoodsunit"
				}
			
				this.list[this.selectindex][obj[this.selectvalue]].produce_time = httpRequest.returnDate(e.value)
				this.timeshow = false
			},
			// 选择生产日期
			selectTime(index, value) {
				this.selectindex = index
				this.selectvalue = value
				this.timeshow = true
			},
			total_price(val1, val2) {
				return val1 * val2
			},
		}
	}
</script>

<style lang="scss" scoped>
	.xiaohuitext {
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.textcontent {
		text-align: center;
	}

	.nostrongtext {
		padding-bottom: 20rpx;
	}

	.allbtn {
		border-radius: 43rpx;
		padding: 10rpx 50rpx;
		border: 2rpx solid #E7E7E7;
	}

	.lansebtn {
		border-radius: 0 43rpx 43rpx 0;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
	}

	.shanchu {
		border-radius: 43rpx 0 0 43rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		border-right: none;
	}

	.shanchu2 {
		border-radius: 43rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.smalltext {
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.sanjiaox {
		border: 14rpx solid transparent;
		border-top: 14rpx solid #666666;
	}

	.inputsl {
		width: 25%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		text-align: center;
	}

	.flexbuju {
		display: flex;
		align-items: flex-start;
	}

	.inputs {
		width: 40%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.xiabiank {
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
</style>
